/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view

import java.awt.Desktop
import com.itextpdf.text.Document
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.Paragraph
import com.itextpdf.text.Chunk
import com.itextpdf.text.pdf.draw.LineSeparator
import com.itextpdf.text.Element
import com.itextpdf.text.pdf.draw.VerticalPositionMark
import com.itextpdf.text.Font
import com.itextpdf.text.Font.FontFamily
import java.text.DecimalFormat


/**
 *
 * @author John Fredy
 */
class GeneratePDF {
    
    def Generate(Order order){
    def document = new Document()
    PdfWriter.getInstance(document, new FileOutputStream("Sales Check.pdf"))
    document.open()
    
    // creates line separators
    LineSeparator UNDERLINE = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
    Chunk tab1 = new Chunk(new VerticalPositionMark(), 80, true);
    Chunk tab2 = new Chunk(new VerticalPositionMark(), 300, true);
    Chunk tab3 = new Chunk(new VerticalPositionMark(), 450, true);
    
    //Title
    Paragraph title = new Paragraph(new Chunk(tab1))
    title.setFont(new Font(FontFamily.HELVETICA, 25, Font.BOLDITALIC))
    title.add("   -----SALES CHECK-----")
    title.setSpacingAfter(30)
    document.add(title)
    
    Paragraph company = new Paragraph("Veluvana Inc.          NIT: 34.234.567", new Font(FontFamily.HELVETICA, 15, Font.ITALIC))
    company.setSpacingAfter(15)
    document.add(company)
        
    //Add buyer data
    Paragraph tiBuyer = new Paragraph()
    tiBuyer.setFont(new Font(FontFamily.HELVETICA, 18, Font.BOLDITALIC))
    tiBuyer.add("BUYER")
    tiBuyer.add(UNDERLINE)
    document.add(tiBuyer)
    //Buyer's name
    Paragraph nameBuyer = new Paragraph("Name: ", new Font(FontFamily.HELVETICA, 15, Font.BOLDITALIC))
    nameBuyer.add(new Chunk(tab1))
    nameBuyer.setFont(new Font(FontFamily.HELVETICA, 13, Font.NORMAL))
    nameBuyer.add(order.getName())
    document.add(nameBuyer)
    //Buyer's address
    Paragraph addressBuyer = new Paragraph("Address: ", new Font(FontFamily.HELVETICA, 15, Font.BOLDITALIC))
    addressBuyer.add(new Chunk(tab1))
    addressBuyer.setFont(new Font(FontFamily.HELVETICA, 13, Font.NORMAL))
    addressBuyer.add(order.getAddress())
    addressBuyer.setSpacingAfter(15)
    document.add(addressBuyer)
    
    //Buyer's product(s)
    Paragraph tiProduct = new Paragraph()
    tiProduct.setFont(new Font(FontFamily.HELVETICA, 18,Font.BOLDITALIC))
    tiProduct.add("PRODUCT(S)")
    tiProduct.add(UNDERLINE)
    document.add(tiProduct)    
    //Products title
    Paragraph titProduct = new Paragraph("Product", new Font(FontFamily.HELVETICA, 15, Font.BOLD))
    titProduct.add(new Chunk(tab2))
    titProduct.add("Amount")
    titProduct.add(new Chunk(tab3))
    titProduct.add("Price")
    titProduct.setSpacingAfter(10)
    document.add(titProduct)    
    
    //Products
    def quantityProduct = order.getQuantity()
    def priceProduct = order.getOrderPrice()
    def df = new DecimalFormat("\$ #,##0.##")
    quantityProduct.each{ 
        if(it.value != 0){
            Paragraph p = new Paragraph()
            switch (it.key) {
                case "cherry": p.add("Cherry") 
                    break
                case "maple": p.add("Curly Maple")
                    break
                case "mahogany": p.add("Genuine Mahogany") 
                    break
                case "wenge": p.add("Wenge") 
                    break
                case "whiteoak": p.add("White Oak")
                    break
                case "sawdust": p.add("Sawdust")
                    break
            }
            p.add(new Chunk(tab2))
            p.add(it.value.toString())
            p.add(new Chunk(tab3))
            p.add(df.format(priceProduct[it.key]))
            document.add(p)  
        }
    }
    
    //Total Price
    Paragraph totalPrice = new Paragraph(new Chunk(tab2))
    totalPrice.setFont(new Font(FontFamily.HELVETICA, 15, Font.BOLDITALIC))
    totalPrice.add("TOTAL PRICE: ")
    totalPrice.add(new Chunk(tab3))
    totalPrice.add(df.format(order.getTotalOrderPrice()))
    totalPrice.setSpacingBefore(10)
    totalPrice.setSpacingAfter(10)
    document.add(totalPrice)
    
    //Delivery Time
    Paragraph time = new Paragraph("Delivery Time is ",new Font(FontFamily.HELVETICA, 15, Font.ITALIC))
    time.add(order.getTotalOrderTime().toString())
    time.add(" Hour(s)")
    time.setSpacingAfter(20)
    document.add(time)

    Paragraph thanks = new Paragraph(new Chunk(tab1))
    thanks.setFont(new Font(FontFamily.HELVETICA, 20, Font.BOLDITALIC))
    thanks.add("THANKS FOR SHOPPING WITH US!")
    document.add(thanks)
        
    document.close()
    Desktop.getDesktop().open(new File("Sales Check.pdf"))
    }
}

